#pragma once

#include "../comm/log.hpp"
#include "../comm/util.hpp"

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <assert.h>
#include <unordered_map>

// 根据题目list文件，加载所有的题目信息到内容中
// model: 主要用来和数据交互，对外提供访问数据的接口

namespace ns_model
{
    using namespace ns_log;
    using namespace ns_util;
    using namespace std;

    struct Question
    {
        std::string number;  // 唯一的题目编号
        std::string title;   // 题目的标题
        std::string star;    // 题目的难度：简单 中等 困难
        int cpu_limit;       // 题目的时间限制 (s)
        int mem_limit;       // 题目的空间限制 (KB)
        std::string desc;    // 题目的描述
        std::string header;  // 题目预设给用户在线编辑的代码
        std::string tail;    // 题目的测试用例代码
    };
    
    const std::string question_list = "./questions/questions.list";
    const std::string question_path = "./questions/";
    class Model 
    {
    private:
        // 题号 和 题目的映射
        unordered_map<string, Question> questions;
    public:
        Model()
        {
            assert(LoadQuestionList(question_list));
        }

        bool LoadQuestionList(const string &question_list)
        {
            // 加载配置文件：./questions/questions.list + 题目编号
            ifstream in(question_list);
            if(!in.is_open())
            {
                LOG(FATAL) << "加载题库失败，请检查是否存在题库文件！" << "\n";
                return false;
            }

            string line;
            while (std::getline(in, line))
            {
                vector<string> tokens;
                StringUtil::StringSpilt(line, &tokens, " ");
                if (tokens.size() != 5) {
                    LOG(WARNING) << "加载部分题目失败！" << "\n";
                    continue;
                }
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = stoi(tokens[3]);
                q.mem_limit = stoi(tokens[4]);

                string path = question_path;
                path += q.number;
                path += "/";

                FileUtil::ReadFile(path + "desc.txt", &(q.desc), true);
                FileUtil::ReadFile(path + "header.cpp", &(q.header), true);
                FileUtil::ReadFile(path + "tail.cpp", &(q.tail), true);

                //LOG(DEBUG) << "编号: " << q.number << "\n" << q.header << "\n";
                questions.insert({q.number, q});
            }

            LOG(INFO) << "加载题库成功..." << "\n";
            in.close();

            return true;
        }

        bool GetAllQuestion(vector<Question> *out)
        {
            if (questions.size() == 0) {
                LOG(ERROR) << "用户加载题库失败！" << "\n";
                return false;
            }

            for (const auto& q: questions)
            {
                out->push_back(q.second);
            }
            return true;
        }

        bool GetOneQuestion(const std::string &number, Question * q)
        {
            const auto &iter = questions.find(number);
            if (iter == questions.end()) {
                LOG(ERROR) << "用户获取题目，题目编号：" << number << "\n";
                return false;
            }
            (*q) = iter->second;
            return true;
        }
        
        ~Model(){}
    };
}